package com.cdu.guoxinan.www.smartcom.service.impl;

import com.cdu.guoxinan.www.smartcom.domian.dto.*;
import com.cdu.guoxinan.www.smartcom.domian.pojo.Page;
import com.cdu.guoxinan.www.smartcom.domian.pojo.User;
import com.cdu.guoxinan.www.smartcom.domian.vo.LoginVO;
import com.cdu.guoxinan.www.smartcom.domian.vo.PageInfo.HomeInfo;
import com.cdu.guoxinan.www.smartcom.domian.vo.PageInfo.LogoInfo;
import com.cdu.guoxinan.www.smartcom.domian.vo.PageInfo.MenuInfo;
import com.cdu.guoxinan.www.smartcom.domian.vo.PageInfo.PageInit;
import com.cdu.guoxinan.www.smartcom.mapper.UserMapper;
import com.cdu.guoxinan.www.smartcom.service.UserService;
import com.cdu.guoxinan.www.smartcom.tool.Tool;
import com.cdu.guoxinan.www.smartcom.unit.model.R;
import com.cdu.guoxinan.www.smartcom.util.JwtUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class UserServiceImpl implements UserService {
    @Value("${HomeInfo.href}")
    private String HomeInfoHref;
    @Value("${HomeInfo.title}")
    private String HomeInfoTitle;
    @Value("${LogoInfo.title}")
    private String LogoInfoTitle;
    @Value("${LogoInfo.image}")
    private String LogoInfoImage;
    @Resource
    private UserMapper userMapper;
    @Resource
    private Tool tool;
    @Override
    public boolean updata(UpdataUserDTO updataUserDTO) {
       return userMapper.updata(updataUserDTO)>0;
    }

    @Override
    public R query(QueryUserDTO queryUserDTO) {
        //设置分页信息
        PageHelper.startPage(queryUserDTO.getPage(), queryUserDTO.getLimit());
        List<User> list = userMapper.query(queryUserDTO);
        PageInfo pageInfo = new PageInfo(list);
        return  R.ok(pageInfo.getList(),pageInfo.getTotal());
    }

    @Override
    public boolean add(User user) {
        return userMapper.add(user)>0;
    }

    @Override
    public LoginVO getLoginVO(User user) {
        //获取用户页面的等级信息
        List<Page> pages = userMapper.getLevel(user.getUserId());
        //封装菜单信息
        HomeInfo homeInfo = new HomeInfo(HomeInfoHref, HomeInfoTitle);
        LogoInfo logoInfo = new LogoInfo(LogoInfoTitle, LogoInfoImage);
        List<MenuInfo> menuInfo = tool.buildMenuTree(pages);
        //检查输出菜单树
        System.out.println(menuInfo);
        LoginVO loginVO = new LoginVO();
        loginVO.setUser( user);
        loginVO.setPageInit(new PageInit(logoInfo,homeInfo,menuInfo));
        String token = JwtUtil.generateToken(user.getUsername(),userMapper.getRole(user.getUserId()) ,24 * 60 * 60 * 1000); // 1天有效
        // 返回token和用户信息
        loginVO.setToken( token);
        return loginVO;
    }

    @Override
    public User queryByUsername(String username) {
         return userMapper.findByUsername(username);
    }

    @Override
    public Integer getUserId(String username) {
        return userMapper.getUserId(username);
    }

    @Override
    public UserProfileDTO getUserProfileById(Long userId) {
        return userMapper.getUserProfileById(userId);
    }

    @Override
    public boolean changeProfile(UserProfileDTO updataUserDTO) {
        return (userMapper.updateUser(updataUserDTO) && userMapper.updateResident(updataUserDTO));
    }

    @Override
    public User queryByeMail(String email) {
        return userMapper.queryByeMail(email);
    }

    @Override
    public boolean resetPassword(ResetDTO resetDTO) {
        return userMapper.resetPassword(resetDTO)>0;
    }
    @Override
    public boolean changePassword(ChangePasswordDTO dto) {
        return userMapper.changePassword(dto) > 0;
    }

    @Override
    public User login(@Validated LoginDTO loginDto) {
        return userMapper.login(loginDto);
    }

    @Override
    public boolean register(registerDTO registerDTO) {
        int result=userMapper.register(registerDTO);
        return result>0;
    }
}
